package com.joker.helloworld.controller;


import com.alibaba.cloud.ai.dashscope.chat.DashScopeChatOptions;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.SimpleLoggerAdvisor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

/**
 * @author 郭鹏伟
 */
@RestController
@RequestMapping("/helloWorld")
public class HelloWordController {

    // ai默认身份
    private static final String DEFAULT_IDENTITY = "你的名字叫小雅，是一个AI助手";

    // ai交互客户端入口
    private final ChatClient chatClient;

//    public HelloWordController(ChatClient.Builder chatClient) {
//        this.chatClient = chatClient
//                // 设置系统提示
//                .defaultSystem(DEFAULT_IDENTITY)
//                .build();
//    }

    public HelloWordController(ChatClient.Builder chatClientBuilder) {
        this.chatClient = chatClientBuilder
                .defaultSystem(DEFAULT_IDENTITY)
                .defaultAdvisors(
                        new SimpleLoggerAdvisor()
                )
                .defaultOptions(
                        DashScopeChatOptions.builder()
                                .withTopP(0.7)
                                .build()
                )
                .build();
    }

    /**
     * chatClient 基础调用
     * @param message 问题
     * @return AI回答
     */
    @GetMapping("/chat/demo")
    public String chat(@RequestParam(value ="message", defaultValue = "你好！可以介绍一下自己吗？") String message) {
        return chatClient.prompt()
                // 用户输入
                .user(message)
                .call()
                // 直接返回文本响应
                .content();
    }

    /**
     * chatClient AI流式调用
     * @param message 问题
     * @return AI回答
     */
    @GetMapping("/chat/stream")
    public Flux<String> chatStream(@RequestParam(value ="message", defaultValue = "你好！可以介绍一下自己吗？") String message, HttpServletResponse response) {
        //设置响应的字符编码为UTF-8
        response.setCharacterEncoding("UTF-8");
        return chatClient.prompt().user(message).stream().content();
    }


    /**
     * advisorChat ChatClient 使用自定义的 Advisor 实现功能增强.管理对话上下文
     * @param message 问题
     * @param id 会话ID
     * @return AI回答
     */
    @GetMapping("/chat/advisor")
    public  Flux<String> advisorChat(@RequestParam(value ="message", defaultValue = "你好！可以介绍一下自己吗？") String message, String id, HttpServletResponse response) {
        //设置响应的字符编码为UTF-8
        response.setCharacterEncoding("UTF-8");
        return this.chatClient.prompt().user(message)
                .advisors(a -> a
                        //设置上下文会话ID
                        .param("CHAT_MEMORY_CONVERSATION_ID_KEY", id)
                        //设置会话历史记录数量,最多检索100条
                        .param("CHAT_MEMORY_RETRIEVE_SIZE_KEY", 100)
                ).stream().content();
    }



}
